<?php

    $servername = "localhost";
    $username = "root";
    $password = "root123";
    $dbname = "oopdb";

    try{
        $conn = new mysqli($servername,$username,$password,$dbname);
    } catch (exception $e) {
        die("连接失败：".$e->getMessage());
    }

    if($conn->connect_error){
        die("连接失败：".$conn->connect_error);
    }
    echo "连接成功！",PHP_EOL;

    //查询
    $select = "select * from fish";
    $result = $conn->query($select);
    $fishrow =[];
//    var_dump($result);
    if ($result->num_rows > 0){
        echo "表里有",$result->num_rows,"条结果",PHP_EOL;

//        $fishrow = $result->fetch_all(MYSQLI_ASSOC);
        while($row = $result->fetch_assoc()) {
            $fishrow [] = $row;
            echo "id:".$row["id"]." name:".$row["name"].
                " weight:".$row["weight"],PHP_EOL;
        }
        echo "<pre>";
        var_dump($fishrow);
    } else {
        echo "没有数据";
    }

////    where查询
//    $select2 = "select * from fish where weight <= 28 order by id limit 5 offset 1";
//    $result2 = $conn->query($select2);
//    $fishrow2 = $result2->fetch_all(MYSQLI_ASSOC);
//    echo "<pre>";
//    var_dump($fishrow2);

    $result->free_result();
    $conn->close();

?>